package com.lyj.cloud.entity;

import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Entity
@Table(name="t_user")
public class User extends BaseIdentity implements Comparable<User> {
    /**
	 * 
	 */
	  private static final long serialVersionUID = 1L;
	  private String username;
	  private String password;
	  /**
	   * 创建时间，仅用作显示
	   */
	  @Transient
	  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	  @Temporal(TemporalType.TIMESTAMP)
	  private Date creationTime;
	  /**
		* 帐号失效时间
		* @hibernate.property
		*/
	  @DateTimeFormat(pattern = "yyyy-MM-dd")
	  @Temporal(TemporalType.DATE)
	  @Transient
	  private Date expireTime;
	  
	  private Integer status;

	  @OneToMany(fetch=FetchType.LAZY, mappedBy="user",orphanRemoval=true,cascade={CascadeType.ALL})
	  private List<UsersRoles> usersRoles;
	  
	  @ManyToOne(fetch=FetchType.LAZY)
	  private Employee employee ;
	  
	  /*
	  @ManyToOne
	  @JoinColumn(name="organizationId",nullable=false)
	  private Organization organization;
	  
	  @ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	  @JoinTable(name="t_role_user", 
				joinColumns={@JoinColumn(name="userId")},
				inverseJoinColumns={@JoinColumn(name="roleId")}
		)
	  private Set<Role> roleUsers = new HashSet<Role>(0);
	  */

	  public User(){
		  
	  }

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreationTime() {
		return creationTime;
	}

	public void setCreationTime(Date creationTime) {
		this.creationTime = creationTime;
	}

	public Date getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public List<UsersRoles> getUsersRoles() {
		return usersRoles;
	}

	public void setUsersRoles(List<UsersRoles> usersRoles) {
		this.usersRoles = usersRoles;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public int compareTo(User o) {
	    return this.id.compareTo(o.id) ;
	  }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((expireTime == null) ? 0 : expireTime.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (expireTime == null) {
			if (other.expireTime != null)
				return false;
		} else if (!expireTime.equals(other.expireTime))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (status == null) {
			if (other.status != null)
				return false;
		} else if (!status.equals(other.status))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", status=" + status + ", password=" + password
				+ ", expireTime=" + expireTime + "]";
	}
	
}